{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Installing hub:\u001b[35m/\u001b[0m\u001b[35m/guardrails/\u001b[0m\u001b[95mregex_match...\u001b[0m\n",
      "✅Successfully installed guardrails/regex_match!\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "! guardrails hub install hub://guardrails/regex_match --quiet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/dtam/dev/guardrails/guardrails/validator_service/__init__.py:85: UserWarning: Could not obtain an event loop. Falling back to synchronous validation.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">ValidationOutcome</span><span style=\"font-weight: bold\">(</span>\n",
       "    <span style=\"color: #808000; text-decoration-color: #808000\">call_id</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'14256398304'</span>,\n",
       "    <span style=\"color: #808000; text-decoration-color: #808000\">raw_llm_output</span>=<span style=\"color: #008000; text-decoration-color: #008000\">\"As of the latest data available, Jupiter has 95 confirmed moons. This number can change as new </span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">moons are discovered and confirmed by astronomers. Jupiter's largest moons, known as the Galilean moons, are Io, </span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">Europa, Ganymede, and Callisto.\"</span>,\n",
       "    <span style=\"color: #808000; text-decoration-color: #808000\">validation_summaries</span>=<span style=\"font-weight: bold\">[]</span>,\n",
       "    <span style=\"color: #808000; text-decoration-color: #808000\">validated_output</span>=<span style=\"color: #008000; text-decoration-color: #008000\">\"As of the latest data available, Jupiter has 95 confirmed moons. This number can change as </span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">new moons are discovered and confirmed by astronomers. Jupiter's largest moons, known as the Galilean moons, are </span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">Io, Europa, Ganymede, and Callisto.\"</span>,\n",
       "    <span style=\"color: #808000; text-decoration-color: #808000\">reask</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span>,\n",
       "    <span style=\"color: #808000; text-decoration-color: #808000\">validation_passed</span>=<span style=\"color: #00ff00; text-decoration-color: #00ff00; font-style: italic\">True</span>,\n",
       "    <span style=\"color: #808000; text-decoration-color: #808000\">error</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span>\n",
       "<span style=\"font-weight: bold\">)</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1;35mValidationOutcome\u001b[0m\u001b[1m(\u001b[0m\n",
       "    \u001b[33mcall_id\u001b[0m=\u001b[32m'14256398304'\u001b[0m,\n",
       "    \u001b[33mraw_llm_output\u001b[0m=\u001b[32m\"As\u001b[0m\u001b[32m of the latest data available, Jupiter has 95 confirmed moons. This number can change as new \u001b[0m\n",
       "\u001b[32mmoons are discovered and confirmed by astronomers. Jupiter's largest moons, known as the Galilean moons, are Io, \u001b[0m\n",
       "\u001b[32mEuropa, Ganymede, and Callisto.\"\u001b[0m,\n",
       "    \u001b[33mvalidation_summaries\u001b[0m=\u001b[1m[\u001b[0m\u001b[1m]\u001b[0m,\n",
       "    \u001b[33mvalidated_output\u001b[0m=\u001b[32m\"As\u001b[0m\u001b[32m of the latest data available, Jupiter has 95 confirmed moons. This number can change as \u001b[0m\n",
       "\u001b[32mnew moons are discovered and confirmed by astronomers. Jupiter's largest moons, known as the Galilean moons, are \u001b[0m\n",
       "\u001b[32mIo, Europa, Ganymede, and Callisto.\"\u001b[0m,\n",
       "    \u001b[33mreask\u001b[0m=\u001b[3;35mNone\u001b[0m,\n",
       "    \u001b[33mvalidation_passed\u001b[0m=\u001b[3;92mTrue\u001b[0m,\n",
       "    \u001b[33merror\u001b[0m=\u001b[3;35mNone\u001b[0m\n",
       "\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from rich import print\n",
    "from guardrails import Guard\n",
    "from guardrails.hub import RegexMatch\n",
    "\n",
    "# Add your OPENAI_API_KEY as an environment variable if it's not already set\n",
    "# import os\n",
    "# os.environ[\"OPENAI_API_KEY\"] = \"YOUR_API_KEY\"\n",
    "\n",
    "guard = Guard().use(RegexMatch(\"95\", match_type=\"search\", on_fail=\"noop\"))\n",
    "\n",
    "response = guard(\n",
    "    model=\"gpt-4o\",\n",
    "    messages=[\n",
    "        {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
    "        {\"role\": \"user\", \"content\": \"How many moons does jupiter have?\"},\n",
    "    ],\n",
    ")\n",
    "\n",
    "print(response)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "litellm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
